package haois.module.leavemsg.rest;

import haois.constant.ReturnState;
import haois.module.leavemsg.daomain.Message;
import haois.module.leavemsg.service.MsgService;
import haois.constant.NetworkUtil;
import haois.module.navigate.daomain.WebSite;
import haois.module.navigate.service.WebSiteService;
import haois.constant.Reply;
import haois.util.RespUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Created by haol on 2017/4/22.
 */
@RestController
@RequestMapping("/msg")
public class MsgController {

    private Logger logger = LoggerFactory.getLogger(MsgController.class);
    @Autowired
    private MsgService msgService;
    @Autowired
    private WebSiteService webSiteService;

    /**
     * @api {POST} /msg/add/{appId} 增加留言信息
     * @apiGroup Message
     * @apiName add
     * @apiDescription 增加一条留言信息
     * @apiParam {String} appId 网站 appId
     * @apiParam {String} linkMan 联系人名字
     * @apiParam {String} phone 电话
     * @apiParam {String} linkAddr 地址
     * @apiParam {String} content 信息
     * @apiParam {String} fromPage 来源
     * @apiParam {String} linkTel 联系电话
     * @apiParam {String} title 标题
     * @apiParamExample {json} 请求样例：
     * POST  /msg/add/{appId}
     * @apiSuccessExample {json} 返回样例:
     * {"code":"00","msg":"增加成功"}
     */
    @RequestMapping(value = "/add/{appId}", method = RequestMethod.POST, params = {"content"})
    public String add(@PathVariable String appId, Message message,
                      HttpServletResponse response, HttpServletRequest request) {
        RespUtil.setACAO(response);
        WebSite webSite = webSiteService.getByAppId(appId);
        if (webSite != null) {
            message.setWebId(webSite.getId());
        }
        message.setAppId(appId);
        message.setIp(NetworkUtil.getIpAddress(request));
        msgService.add(message);
        logger.info("IP地址为[{}]的用户增加信息[{}]", message.getIp(),message.getContent());
        return new Reply(ReturnState.CODE_00, "增加成功").toJson();
    }

    /**
     * @api {GET} /msg/list 查看全部的留言信息
     * @apiGroup Message
     * @apiName getMsg
     * @apiDescription 获取全部的留言信息
     * @apiParamExample {json} 请求样例：
     * GET /msg/list
     * @apiSuccessExample {json} 返回样例:
     * {"code":0,"msg":"success","data":[{"id":13,"name":"张睿浩","time":"2017-04-30 00:59:13","ip":"120.236.146.233","message":"please call me","look":0,"phone":"17090077783","site":"rescume","soft":"index/recume"}]}
     */
    @RequestMapping(value = "/list")
    public String getMsg(HttpServletRequest request, HttpServletResponse response) {
        RespUtil.setACAO(response);
        List<Message> messages = msgService.getMsg();
        logger.info("IP地址为[{}]请求留言数据", NetworkUtil.getIpAddress(request));
        Reply<List<Message>> reply = new Reply<List<Message>>(ReturnState.CODE_00, "success", messages);
        return reply.toJson();
    }

    /**
     * @api {GET} /msg/i/see/{id} 获取留言byId
     * @apiGroup Message
     * @apiName getMsgById
     * @apiDescription 根据id获取该留言的详细信息
     * @apiParam {Integer} id 留言id
     * @apiParamExample {json} 请求样例：
     * GET /msg/i/see/{id}
     * @apiSuccessExample {json} 返回样例:
     * {"code":0,"msg":"success","data":{"id":13,"name":"张睿浩","time":"2017-04-30 00:59:13","ip":"120.236.146.233","message":"please call me","look":0,"phone":"17090077783","site":"rescume","soft":"index/recume"}}
     */
    @RequestMapping(value = "see/{id}")
    public String getMsgById(@PathVariable Integer id, HttpServletResponse response) {
        RespUtil.setACAO(response);
        Message message = msgService.getById(id);
        logger.info("客户端请求id为[{}]的留言数据", id);
        logger.info(message.toString());
        Reply<Message> reply = new Reply<Message>(ReturnState.CODE_00, "success", message);
        return reply.toJson();
    }

    /**
     * @api {GET} /msg/del/{id} 删除留言ById
     * @apiGroup Message
     * @apiName getByNum
     * @apiDescription 根据网页名获取该网页的访问次数
     * @apiParam {Integer} id 留言id
     * @apiParamExample {json} 请求样例：
     * GET /msg/del/{id}
     * @apiSuccessExample {json} 返回样例:
     * {"true"}
     */
    @RequestMapping(value = "/del/{id}")
    public String  delMsgById(@PathVariable Integer id, HttpServletResponse response) {
        RespUtil.setACAO(response);
        msgService.delete(id);
        logger.info("客户端删除id为[{}]留言数据", id);
        Reply<Message> reply = new Reply<Message>(ReturnState.CODE_00, "删除成功");
        return reply.toJson();
    }

    /**
     * @api {GET} /msg/list/{appId} 获取网站留言ByAppId
     * @apiGroup Message
     * @apiName getMsgByAppId
     * @apiDescription 根据appId请求该网站的全部留言
     * @apiParamExample {json} 请求样例：
     * GET /msg/list/{appId}
     * @apiSuccessExample {json} 返回样例:
     * {"code":0,"msg":"success","data":[{"id":13,"name":"张睿浩","time":"2017-04-30 00:59:13","ip":"120.236.146.233","message":"please call me","look":0,"phone":"17090077783","site":"rescume","soft":"index/recume"}]}
     */
    @RequestMapping(value = "/list/{appId}")
    public String getMsgByAppId(@PathVariable String appId, HttpServletRequest request, HttpServletResponse response) {
        RespUtil.setACAO(response);
        List<Message> messages = msgService.getMsgByAppId(appId);
        logger.info("IP地址为[{}]请求留言数据", NetworkUtil.getIpAddress(request));
        Reply<List<Message>> reply = new Reply<List<Message>>(ReturnState.CODE_00, "success", messages);
        return reply.toJson();
    }

}
